/**
 * 
 */
package dms.presentation.action.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dms.dto.CampusDTO;
import dms.model.Campus;
import dms.model.util.PageIterator;
import dms.service.CampusService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.PageUtil;

/**
 * @author 李健
 *
 */
public class CampusAction extends DispatchAction {
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CampusService campusService = (CampusService) ContextUtil.getContext().getBean("campusService");
		
		int[] params = PageUtil.parsePageParameter(request);
		int start = params[0];
		int count = params[1];
		
		PageIterator<Campus> campuses = campusService.findAll(start, count);
		request.setAttribute("campuses", campuses);
		
		return mapping.findForward("list");
	}
	
	public ActionForward addForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		return mapping.findForward("addForm");
		
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CampusService campusService = (CampusService) ContextUtil.getContext().getBean("campusService");
		
		CampusDTO dto = (CampusDTO) form;
		
		campusService.createCampus(dto);
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward updateForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Long campusId = null;
		CampusService campusService = (CampusService) ContextUtil.getContext().getBean("campusService");
		try {
			campusId = Long.valueOf(request.getParameter("id"));
		} catch (Exception e) {
			log.error("Campus Id 解析错误");
		}
		Campus campus = campusService.findById(campusId);
		request.setAttribute("campus", campus);
		
		return mapping.findForward("updateForm");
		
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CampusDTO dto = (CampusDTO) form;
		CampusService campusService = (CampusService) ContextUtil.getContext().getBean("campusService");
		campusService.updateCampus(dto);
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Long campusId = null;
		CampusService campusService = (CampusService) ContextUtil.getContext().getBean("campusService");
		try {
			campusId = Long.valueOf(request.getParameter("id"));
		} catch (Exception e) {
			log.error("Campus Id 解析错误");
		}
		
		campusService.deleteCampusById(campusId);
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		return this.list(mapping, form, request, response);
	}
}
	
	
